package org.hit.burkun.umls;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.hit.burkun.db.DBHelper;
import org.hit.burkun.entities.Entity;

public class DoUmlsMapper extends BaseMapper {

	private String otherCondition = "xref_dbname = 'UMLS_CUI'";
	private MapidsUmlsDump mapDbh;
	private Entity.TYPE type = Entity.TYPE.DISEASE_ONTOLOGY;
	private Set<String> mapped = new HashSet<>();
	private Set<String> allids = new HashSet<>();

	public DoUmlsMapper(DBHelper dbh, String tbName, MapidsUmlsDump toDbh) {
		super(dbh, tbName);
		this.mapDbh = toDbh;
	}

	public DoUmlsMapper(DBHelper dbh, MapidsUmlsDump toDbh) {

		this(dbh, "do_term_dbxref", toDbh);
	}

	public void quickMap(Collection<String> ids) {
		map(ids, "xref_key", "term_id", otherCondition);
	};

	@Override
	protected void map(java.util.Collection<String> ids, String conceptFiled,
			String codeFiled, String otherCondition) {
		allids.addAll(ids);
		super.map(ids, conceptFiled, codeFiled, otherCondition);

	};

	@Override
	public void process(Map<String, Set<String>> data) {
		mapped.addAll(data.keySet());
		saveMappedToDbAdapter(type, data);
	}

	// 缓存id 到 doterm
	private Map<String, String> doTermIdMap;

	public Map<String, String> getAllGoData(DBHelper dbh) {
		dbh.connectionDB();
		String queryStr = "select primary_id, acc from do_term where is_obsolete = 0";
		Collection<String[]> res = dbh.queryData(queryStr, 2);
		Map<String, String> hashMap = new HashMap<String, String>();
		for (String[] line : res) {
			hashMap.put(line[0], line[1]);
		}
		dbh.disconnection();
		doTermIdMap = hashMap;
		return hashMap;
	}

	// 更改
	private void saveMappedToDbAdapter(Entity.TYPE type,
			Map<String, Set<String>> data) {
		Map<String, Set<String>> hashMap = new HashMap<String, Set<String>>();
		Set<Entry<String, Set<String>>> enties = data.entrySet();
		for (Entry<String, Set<String>> entry : enties) {
			String key = entry.getKey();
			hashMap.put(doTermIdMap.get(key), entry.getValue());
		}
		mapDbh.saveMappedToDb(type, hashMap);
	}

	// 更改
	private void saveUnmappedToDbAdapter(Entity.TYPE type,
			Collection<String> data) {
		Collection<String> res = new LinkedList<String>();
		for (String id : data) {
			res.add(doTermIdMap.get(id));
		}
		mapDbh.saveUnMappedToDb(type, res);

	}

	public Map<String, String> getAllGoData() {
		Map<String, String> res = getAllGoData(this.dbh);
		return res;
	}

	@Override
	public void end() {
		allids.removeAll(mapped);
		saveUnmappedToDbAdapter(type, allids);
		super.end();
	}

	public static void main(String[] args) {
		DBHelper dbh =  DBHelper.getServerData();
		MapidsUmlsDump mapDbh = new MapidsUmlsDump(
				 DBHelper.getServerData(), "map_umls_ids");
		DoUmlsMapper mapper = new DoUmlsMapper(dbh, mapDbh);
		mapper.begin();
		Map<String, String> data = mapper.getAllGoData( DBHelper.getServerData());
		mapper.quickMap(data.keySet());
		mapper.end();
	}
}
